#include "stdafx.h"
#include "CGrass.h"
#include <fstream>
#include <iostream>
using namespace std;

CGrass::CGrass()
{
}


CGrass::~CGrass()
{
}

void CGrass::LoadImg()
{
	m_grassimg = cvLoadImage("./picture/grass0.jpg");
}

void CGrass::ReadGrasstxt()
{
	fstream grasspos;
	grasspos.open("./grassloc.txt");
	for (int i = 0; i < 11; i++)
	{
		for (int j = 0; j < 11; j++)
		{
			grasspos >> m_grassloc[i][j];
		}
	}
	grasspos.close();
}

void CGrass::DrawtoBk(IplImage * pbk,int x,int y)
{
	LoadImg();
	if (pbk != NULL && m_grassimg != NULL)
	{
		for (int i = 0; i < m_grassimg->height; i++)
		{
			for (int j = 0; j < m_grassimg->width; j++)
			{
				int bch = CV_IMAGE_ELEM(m_grassimg, uchar, i, j * 3 + 0);
				int gch = CV_IMAGE_ELEM(m_grassimg, uchar, i, j * 3 + 1);
				int rch = CV_IMAGE_ELEM(m_grassimg, uchar, i, j * 3 + 2);
				if (!(bch > 245 && gch > 245 && rch > 245))
				{
					CV_IMAGE_ELEM(pbk, uchar, x + i, (j + y) * 3 + 0) = CV_IMAGE_ELEM(m_grassimg, uchar, i, j * 3 + 0);
					CV_IMAGE_ELEM(pbk, uchar, x + i, (j + y) * 3 + 1) = CV_IMAGE_ELEM(m_grassimg, uchar, i, j * 3 + 1);
					CV_IMAGE_ELEM(pbk, uchar, x + i, (j + y) * 3 + 2) = CV_IMAGE_ELEM(m_grassimg, uchar, i, j * 3 + 2);
				}
			}
			
		}
	}
	
}
